
#import "PUHomeReusableView.h"
#import "SDCycleScrollView.h"
#import "PUMenuCollectionCell.h"
#import "PUTopCollectionCell.h"
#import "PUFoodsModel.h"

@interface PUHomeReusableView() <SDCycleScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectView1;
@property (nonatomic, strong) UICollectionView *collectView2;
@property (nonatomic, copy) NSArray *menuArray;
@property (nonatomic, copy) NSArray *dataArray;
@property (nonatomic, copy) NSArray *bannerArray;
@property (nonatomic, assign) NSInteger menuIndex;
@end

@implementation PUHomeReusableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    self.menuArray = @[@"全部",@"川菜",@"点心",@"烘焙",@"家常菜",@"面食",@"粤菜"];
    
    UILabel *titleLabel1 = [[UILabel alloc]init];
    titleLabel1.font = [UIFont boldSystemFontOfSize:16];
    titleLabel1.text = @"今日推荐";
    [self addSubview:titleLabel1];
    [titleLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@15);
        make.top.equalTo(@15);
    }];
    
    UIView *lineView1 = [[UIView alloc]init];
    lineView1.backgroundColor = [UIColor colorWithHex:0xFF729A];
    lineView1.clipsToBounds = true;
    lineView1.layer.cornerRadius = 5/2;
    [self addSubview:lineView1];
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel1.mas_bottom).offset = 3;
        make.centerX.equalTo(titleLabel1);
        make.width.offset = 16;
        make.height.offset = 5;
    }];
    
    PUFoodsModel *model1 = PUFoodsModels.shared.list[0];
    PUFoodsModel *model2 = PUFoodsModels.shared.list[1];
    PUFoodsModel *model3 = PUFoodsModels.shared.list[2];
    self.bannerArray = @[model1.foods.firstObject,model2.foods.firstObject,model3.foods.firstObject];
    
    NSArray *imagesURLStrings = [self.bannerArray valueForKey:@"cover"];

    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(_window_width*0.042, 50, _window_width*0.914, 200) delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
    cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    //    cycleScrollView.titlesGroup = titles;
    cycleScrollView.currentPageDotColor = [UIColor whiteColor];
    cycleScrollView.backgroundColor=[UIColor whiteColor];
    cycleScrollView.imageURLStringsGroup = imagesURLStrings;
    cycleScrollView.layer.cornerRadius = 10;
    cycleScrollView.layer.masksToBounds = YES;
    [self addSubview:cycleScrollView];
    
    UILabel *titleLabel2 = [[UILabel alloc]init];
    titleLabel2.text = @"热门菜谱";
    titleLabel2.font = [UIFont boldSystemFontOfSize:16];
    [self addSubview:titleLabel2];
    [titleLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@15);
        make.top.equalTo(cycleScrollView.mas_bottom).offset(15);
    }];
    
    UIView *lineView2 = [[UIView alloc]init];
    lineView2.backgroundColor = [UIColor colorWithHex:0xFF729A];
    lineView2.clipsToBounds = true;
    lineView2.layer.cornerRadius = 5/2;
    [self addSubview:lineView2];
    [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel2.mas_bottom).offset = 3;
        make.centerX.equalTo(titleLabel2);
        make.width.offset = 16;
        make.height.offset = 5;
    }];
    
    UICollectionViewFlowLayout *flowLayout1 = [[UICollectionViewFlowLayout alloc] init];
    flowLayout1.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout1.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
    flowLayout1.estimatedItemSize = CGSizeMake(72, 25);
    self.collectView1 = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout1];
    //          self.collectView.showsHorizontalScrollIndicator = NO;
    self.collectView1.delegate = self;
    self.collectView1.dataSource = self;
    self.collectView1.backgroundColor =[UIColor clearColor];
    self.collectView1.showsVerticalScrollIndicator = false;
    self.collectView1.showsHorizontalScrollIndicator = false;
    [self addSubview:self.collectView1];
    
    [self.collectView1 registerClass:[PUMenuCollectionCell class] forCellWithReuseIdentifier:@"CPMenuCollectionCell"];
    
    [self.collectView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel2.mas_bottom).offset(10);
        make.left.right.offset(0);
        make.height.offset(35);
    }];
    
    UILabel *topLabel = [[UILabel alloc]init];
    topLabel.text = @"T\nO\nP\n3";
    topLabel.numberOfLines = 0;
    topLabel.textAlignment = NSTextAlignmentCenter;
    topLabel.font = [UIFont boldSystemFontOfSize:16];
    topLabel.textColor = UIColor.whiteColor;
    topLabel.backgroundColor = [UIColor colorWithHex:0xFF729A];
    topLabel.layer.cornerRadius = 10;
    topLabel.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMinXMaxYCorner;
    topLabel.layer.masksToBounds = true;
    [self addSubview:topLabel];
    [topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset = 15;
        make.top.equalTo(self.collectView1.mas_bottom).offset = 10;
        make.width.offset = 44;
        make.height.offset = 132;
    }];
    
    UIButton *moreButton = [[UIButton alloc]init];
    [moreButton setTitle:@">>" forState:UIControlStateNormal];
    [moreButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [moreButton addTarget:self action:@selector(moreAction) forControlEvents:UIControlEventTouchUpInside];
    moreButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    moreButton.titleLabel.font = [UIFont systemFontOfSize:14];
    moreButton.backgroundColor = [UIColor colorWithHex:0xFF729A];
    moreButton.layer.cornerRadius = 10;
    moreButton.layer.maskedCorners = kCALayerMaxXMinYCorner | kCALayerMaxXMaxYCorner;
    moreButton.layer.masksToBounds = true;
    [self addSubview:moreButton];
    [moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset = -15;
        make.top.equalTo(topLabel);
        make.width.offset = 33;
        make.height.offset = 132;
    }];
    
    UICollectionViewFlowLayout *flowLayout2 = [[UICollectionViewFlowLayout alloc] init];
    flowLayout2.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout2.sectionInset = UIEdgeInsetsMake(8, 8, 8, 8);
    flowLayout2.itemSize = CGSizeMake(116, 116);
    self.collectView2 = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout2];
    self.collectView2.delegate = self;
    self.collectView2.dataSource = self;
    self.collectView2.backgroundColor =[UIColor whiteColor];
    self.collectView2.showsVerticalScrollIndicator = false;
    self.collectView2.showsHorizontalScrollIndicator = false;
    [self addSubview:self.collectView2];
    
    [self.collectView2 registerClass:[PUTopCollectionCell class] forCellWithReuseIdentifier:@"CPTopCollectionCell"];
    
    [self.collectView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topLabel);
        make.left.equalTo(topLabel.mas_right);
        make.right.equalTo(moreButton.mas_left);
        make.height.offset(132);
    }];
    
    UILabel *titleLabel3 = [[UILabel alloc]init];
    titleLabel3.text = @"邻家菜谱";
    titleLabel3.font = [UIFont boldSystemFontOfSize:16];
    [self addSubview:titleLabel3];
    [titleLabel3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.top.equalTo(self.collectView2.mas_bottom).offset = 15;
    }];
    
    UIView *lineView3 = [[UIView alloc]init];
    lineView3.backgroundColor = [UIColor colorWithHex:0xFF729A];
    lineView3.clipsToBounds = true;
    lineView3.layer.cornerRadius = 5/2;
    [self addSubview:lineView3];
    [lineView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel3.mas_bottom).offset = 3;
        make.centerX.equalTo(titleLabel3);
        make.width.offset = 16;
        make.height.offset = 5;
    }];
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView == self.collectView1) {
        return self.menuArray.count;
    }
    else {
        return self.dataArray.count;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == self.collectView1) {
        static NSString *identifier = @"CPMenuCollectionCell";
        PUMenuCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        cell.selectView.backgroundColor = self.menuIndex == indexPath.row ? [UIColor colorWithHex:0xFF729A] : UIColor.clearColor;
        cell.titleLabel.textColor = self.menuIndex == indexPath.row ? [UIColor whiteColor] : UIColor.blackColor;
        cell.titleLabel.text = self.menuArray[indexPath.row];
        return cell;
    }
    else {
        static NSString *identifier = @"CPTopCollectionCell";
        PUTopCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        PUFoodModel *model = self.dataArray[indexPath.row];
        
        cell.model = model;
        if (indexPath.row == 0) {
            cell.topImageView.image = [UIImage imageNamed:@"第一"];
        }
        else if (indexPath.row == 1) {
            cell.topImageView.image = [UIImage imageNamed:@"第二"];
        }
        else if (indexPath.row == 2) {
            cell.topImageView.image = [UIImage imageNamed:@"第三"];
        }
        else {
            cell.topImageView.image = nil;
        }
        return cell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.collectView1) {
        
        self.menuIndex = indexPath.row;
        [self.collectView1 reloadData];
        
        [self reloadViewData];
    }
    else {
        PUFoodModel *model = self.dataArray[indexPath.row];
        if (self.topIndexBlock) {
            self.topIndexBlock(model);
        }
    }
}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    PUFoodModel *model = self.bannerArray[index];
    if (self.topIndexBlock) {
        self.topIndexBlock(model);
    }
}

- (void)moreAction {
    if (self.meunIndexBlock) {
        self.meunIndexBlock(0);
    }
}

- (void)setAllData:(NSMutableArray *)allData {
    _allData = allData;
    [self reloadViewData];
}

- (void)reloadViewData {
    if (self.menuIndex == 0) {
        NSMutableArray *array = [NSMutableArray array];
        for (PUFoodsModel *model in self.allData) {
            [array addObjectsFromArray:model.foods];
        }
        self.dataArray = array;
    }
    else {
        PUFoodsModel *model = self.allData[self.menuIndex-1];
        self.dataArray = [model.foods mutableCopy];
    }
    [self.collectView2 reloadData];
}

@end
